﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace BlueLotus7.ArchiveSystem.SinaContentApi.Weibo
{
    partial class ContentAnalyzer
    {
        #region Qun info

        public static List<KeyValuePair<string, string>> TranslateWeiboUserQunList(string htmlContent)
        {
            List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

            if (!string.IsNullOrWhiteSpace(htmlContent))
            {
                htmlContent = CleanHtmlContent(htmlContent);
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(htmlContent);

                TranslateWeiboUserQunList(xmlDocument.DocumentElement, result);
            }

            return result;
        }

        private static void TranslateWeiboUserQunList(XmlNode bodyXml, List<KeyValuePair<string, string>> qunListContainer)
        {
            const string groupId = "groupid";
            if (bodyXml != null && bodyXml.Name == "body")
            {
                XmlNode linkContainerNode = bodyXml.SelectSingleNode("descendant::div[a and @class='c']");
                foreach (XmlNode one in linkContainerNode.SelectNodes("descendant::a"))
                {
                    Dictionary<string, string> parameters = TranslateUrlParameters(GetXmlAttributeValue(one, tag_href));
                    if (parameters.ContainsKey(groupId))
                    {
                        qunListContainer.Add(new KeyValuePair<string, string>(parameters[groupId], one.InnerText));
                    }
                }
            }
        }

        #endregion
    }
}
